import argparse, ipaddress, json, os

def str_to_bool(value: str) -> bool:
    # 同上定义的转换函数
    if isinstance(value, bool):
        return value
    value = value.strip().lower()
    if value in ('true', 't', 'yes', 'y', '1'):
        return True
    elif value in ('false', 'f', 'no', 'n', '0'):
        return False
    else:
        return False
PARSER = argparse.ArgumentParser()
PARSER.add_argument('-e', '--env', type=str, default='loc', help='设置应用环境 (loc, dev, pre, pro)')
# PARSER.add_argument('-d', '--debug-key', type=str, default=None, help='输入KEY启用调试日志')
PARSER.add_argument('-n', '--output-name', type=str, default=None, help='指定输出名称')
PARSER.add_argument('-o', '--output-dir', type=str, default=None, help='指定输出目录')
PARSER.add_argument('-r', '--res-file', type=str, default=None, help='指定OPENAPI请求结果保存文件')
PARSER.add_argument('-s', '--subject', type=str, default=None, help='主题')
PARSER.add_argument('-k', '--deepseek-key', type=str, default=None, help='DeepSeek API KEY')
ARGS = PARSER.parse_args()

APP_ENV = ARGS.env
print(f'env: {APP_ENV}')

# inc
DEBUG = False
DEBUG_KEY = 'dp123456'
# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
LOGS_DIR = f'{os.sep}opt{os.sep}logs'
DATA_DIR = f'{os.sep}opt{os.sep}data'
APP_NAME = 'dp'
APP_PRIVATE_KEY = ""
APP_PUBLIC_KEY = ""
APP_ALGORITHM = "RS256"
APP_TOKEN_LITETIME = 86400
APP_SCHEDULER_THREAD_COUNT = 10
APP_SCHEDULER_PROCESS_COUNT = 4
APP_HOST = '0.0.0.0'
APP_PORT = 12380
OUTPUT_NAME = 'dp'
DEEPSEEK_KEY = ""
SUBJECT = ""
OUTPUT_DIR = f"{DATA_DIR}{os.sep}{OUTPUT_NAME}"
RESPONSE_FILE = f"{DATA_DIR}{os.sep}response.json"
MIDDLE_FILE = f"{OUTPUT_DIR}{os.sep}{OUTPUT_NAME}.middle"
DOCPRESS_FILE = f"{OUTPUT_DIR}{os.sep}{OUTPUT_NAME}.xml"

# loc
LOGS_DIR = f'{BASE_DIR}{os.sep}logs'
DATA_DIR = f'{BASE_DIR}{os.sep}data'
OUTPUT_DIR = f"{DATA_DIR}{os.sep}{OUTPUT_NAME}"
RESPONSE_FILE = f"{DATA_DIR}{os.sep}response.json"
MIDDLE_FILE = f"{OUTPUT_DIR}{os.sep}{OUTPUT_NAME}.middle"
DOCPRESS_FILE = f"{OUTPUT_DIR}{os.sep}{OUTPUT_NAME}.xml"

# custom
def get_output_name() :
    output_name = OUTPUT_NAME
    if ARGS.output_name :
        output_name = ARGS.output_name
    print(f'output_name: {output_name}')
    return output_name
def get_output_dir() :
    output_dir = OUTPUT_DIR
    if ARGS.output_dir :
        output_dir = ARGS.output_dir
        if output_dir[0:1] == '/' :
            pass
        else :
            output_dir = f'{BASE_DIR}{os.sep}{output_dir}'
    print(f'output_dir: {output_dir}')
    return output_dir
def get_response_file() :
    res_file = RESPONSE_FILE
    if ARGS.res_file :
        res_file = ARGS.res_file
        if res_file[0:1] == '/' :
            pass
        else :
            res_file = f'{BASE_DIR}{os.sep}{res_file}'
    print(f'response_file: {res_file}')
    return res_file
def get_middle_file() :
    return f"{OUTPUT_DIR}{os.sep}{OUTPUT_NAME}.middle"
def get_docpress_file() :
    return f"{OUTPUT_DIR}{os.sep}{OUTPUT_NAME}.xml"
def get_subject() :
    subject = SUBJECT
    if ARGS.subject and ARGS.subject != '':
        subject = ARGS.subject
    print(f'subject: {subject}')
    return subject

def get_deepseek_key(): 
    deepseek_key = DEEPSEEK_KEY
    if ARGS.deepseek_key and ARGS.deepseek_key != '':
        deepseek_key = ARGS.deepseek_key
    print(f'deepseek_key: {deepseek_key}')
    return deepseek_key

OUTPUT_NAME = get_output_name()
OUTPUT_DIR = get_output_dir()
RESPONSE_FILE = get_response_file()
# print(f'RESPONSE_FILE: {RESPONSE_FILE}')
MIDDLE_FILE = get_middle_file()
DOCPRESS_FILE = get_docpress_file()
SUBJECT = get_subject()
DEEPSEEK_KEY = get_deepseek_key()
